package cn.aeolia.dao;

import cn.aeolia.pojo.Department;
import cn.aeolia.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;


/**
 * 员工表dao
 *
 * @author aeolia
 */
@Repository("employeeDao")
public class EmployeeDao {

    private static Map<Integer, Employee> employeeMap = null;
    //模拟数据库中的数据
    /**
     * 主键自增
     */
    private static Integer initID = 1006;

    static {
        //创建一个员工表
        employeeMap = new HashMap<>();


        employeeMap.put(101, new Employee(1001, "AA", 0, "483489763@qq.com", new Department(101, "后勤部")));
        employeeMap.put(102, new Employee(1002, "BB", 1, "483489763@qq.com", new Department(101, "教学部")));
        employeeMap.put(103, new Employee(1003, "CC", 0, "483489763@qq.com", new Department(101, "教研部")));
        employeeMap.put(104, new Employee(1004, "DD", 1, "483489763@qq.com", new Department(101, "运营部")));
        employeeMap.put(105, new Employee(1005, "EE", 0, "483489763@qq.com", new Department(101, "市场部")));

    }

    /**
     * 员工有所属的部门
     */
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 获得所有员工信息
     *
     * @return
     */
    public Collection<Employee> getDepartments() {
        return employeeMap.values();
    }

    /**
     * 通过id得到员工
     *
     * @param id
     * @return
     */
    public Employee getDepartmentsById(Integer id) {
        return employeeMap.get(id);
    }

    /**
     * 增加一个员工
     *
     * @param employee
     */
    public void save(Employee employee) {
        try {
            if (employee.getId() == null) {
                employee.setId(initID);
            }
            employee.setDepartment(departmentDao.getDepartmentsById(employee.getDepartment().getId()));

            employeeMap.put(employee.getId(), employee);
            initID++;
        } catch (Exception e) {
            initID--;
            e.printStackTrace();
        }
    }

    /**
     * 根据id删除employee
     *
     * @param id
     */
    public void deleteEmployeeById(Integer id) {
        employeeMap.remove(id);
    }

    /**
     * 根据Employee对象删除
     *
     * @param employee
     */
    public void deleteEmployee(Employee employee) {
        Integer employeeId = employee.getId();
        Employee employee1 = employeeMap.get(employeeId);
        if (employee1.equals(employee1)) {
            employeeMap.remove(employeeId);
        }
    }

    /**
     * 删除一堆Employee
     *
     * @param employees
     */
    public void deleteEmployees(List<Employee> employees) {
        for (Employee employee : employees) {
            deleteEmployee(employee);
        }
    }

    /**
     * 查询所有的Employee
     *
     * @return
     */
    public List<Employee> getAllEmployee() {
        List<Employee> employees = new ArrayList<>();
        for (Integer key : employeeMap.keySet()) {
            employees.add(employeeMap.get(key));
        }
        return employees;
    }


}
